package com.jhd.jiangxi.activity.scan.in.supply;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.tabs.TabLayout;
import com.jhd.jiangxi.R;
import com.jhd.jiangxi.UtilHelper;
import com.jhd.jiangxi.activity.main.BaseActivity;
import com.jhd.jiangxi.entity.LastInputEditText;
import com.jhd.jiangxi.service.GetVaccineService;
import com.jhd.jiangxi.util.PopWindow;
import com.jhd.jiangxi.util.ScanUtil;
import com.jhd.jiangxi.vo.Approval;
import com.jhd.jiangxi.vo.ElectCodePackageVo;
import com.jhd.jiangxi.vo.GlobalDictionary;
import com.jhd.jiangxi.vo.JsonForResult;
import com.jhd.jiangxi.vo.RefDept;
import com.jhd.jiangxi.vo.SuperiorList;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.jhd.jiangxi.activity.scan.in.supply.SupplyStorageActivity.electCodePackageVos;


/*供应入库*/
public class SupplyActivity extends BaseActivity implements View.OnClickListener, SupplyBillFragment.OnFragmentInteractionListener, SupplyContentFragment.OnFragmentInteractionListener {

    private ViewPager vp;
    private List<Fragment> mDatas = new ArrayList<Fragment>();
    protected SupplyBillFragment supplyBillFragment;
    protected SupplyContentFragment supplyContentFragment;
    private TabLayout tabs;
    private FragmentPagerAdapter adapter;
    private TextView btnOrderNo, btnSupplyDept, btnVaccineType, btnPriceBook, btnDelAllVaccine, btnSubmitVaccine;
    private ProgressDialog _progressDialog = null;
    private final List<String> orderNo = new ArrayList<String>();
    private final List<String> id = new ArrayList<String>();
    private final List<String> name = new ArrayList<String>();
    private final List<String> deptId = new ArrayList<String>();
    private final List<String> category = new ArrayList<String>();
    private final List<String> sourceType = new ArrayList<String>();
    private final List<String> createTime = new ArrayList<String>();
    private final List<String> sourceId = new ArrayList<String>();
    private final List<String> transformUnit = new ArrayList<String>();
    private final List<String> transformUnitName = new ArrayList<String>();
    private AlertDialog _alertDialog = null;
    private int index = 0;
    private BarcodeDataReceiver barcodeDataReceiver = null;
    public static List<ElectCodePackageVo> electCodePackageVos2 = new ArrayList<ElectCodePackageVo>();//已经扫描到的所有
    public static List<ElectCodePackageVo> electCodePackageVos3 = new ArrayList<ElectCodePackageVo>();//该储位扫描到的
    private RecyclerView rvStorage;
    private RecyclerAdapter recyclerAdapter;
    private MyBroadcastReceiver broadcast = null;
    private List<Approval> approvalList = null;
    private Context context = SupplyActivity.this;
    private ListView listView;
    private Button btnToTemp;
    private String orderName;//选中订单的名称
    private List<SuperiorList> superiorList=null;
    private TextView btnStartTime,btnEndTime,btnTransportEquip;
    private LastInputEditText btnDeliveryDept;
    private boolean isCheck=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply);
        getBack(context);
        getOrderNo();
        getDept();//进入界面先获取供货部门的列表
        getPriceBookMst();//获取价格本列表
        getVaccineDistributionDempartByDeptId();//进入界面先获取配送单位的列表
        getApprovalList("供应入库");//获取审批流列表
        getGlobalDictionaryByType("冷链设备类型");//获取运输设备
        openScan(context);
        initView();
        initEvent();
    }


    public void getBack(Context context) {//
        broadcast = new MyBroadcastReceiver();
        IntentFilter mFilter = new IntentFilter("com.jhd.test.broadcast");
        context.registerReceiver(broadcast, mFilter);
    }

    public void getBackClose(Context context) {//
        if (broadcast != null) {
            context.unregisterReceiver(broadcast);
        }
    }

    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getStringExtra("type").equals("0")) {
                if (vp.getCurrentItem() == 1) {
                    vp.setCurrentItem(0);
                    ScanUtil.powerOffScan(context);
                }
            } else if (intent.getStringExtra("type").equals("1")) {
                finish();
            } else if (intent.getStringExtra("type").equals("2")) {
                recyclerAdapter = new RecyclerAdapter(electCodePackageVos2);
                rvStorage.setAdapter(recyclerAdapter);
            }
        }
    }
    private boolean isContains(List<ElectCodePackageVo> electCodePackageVoList,String code){
        for (ElectCodePackageVo electCodePackageVo : electCodePackageVoList) {
            if(electCodePackageVo.getElectCode().equals(code)){
                return true;
            }
        }
        return false;
    }

    class BarcodeDataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String scanResult_1 = intent.getStringExtra("SCAN_BARCODE1");
            final String scanStatus = intent.getStringExtra("SCAN_STATE");

            if ("ok".equals(scanStatus)) {
                ScanUtil.stopScan(context);
                if(isContains(electCodePackageVos,scanResult_1)){
                    int position = application.getPosition();
                    for (int i = 0; i < electCodePackageVos.size(); i++) {
                        if (electCodePackageVos.get(i).getElectCode().equals(scanResult_1)) {
                            getVaccineInfoByCodeSingle(scanResult_1);
                            ElectCodePackageVo electCodePackageVo = electCodePackageVos.get(i);
                            electCodePackageVo.setPosition(position);
                            electCodePackageVo.setStorage(application.getStorageName());
                            electCodePackageVo.setStorageId(application.getStorageId());
                            if(application.getContractNo()!=null)electCodePackageVo.setContractNo(application.getContractNo());
                            electCodePackageVos2.add(electCodePackageVo);
                            electCodePackageVos3.add(electCodePackageVo);
                            electCodePackageVos.remove(i);
                            if (electCodePackageVos3.size() == 1) {
                                recyclerAdapter = new RecyclerAdapter(electCodePackageVos3);
                                rvStorage.setAdapter(recyclerAdapter);
                            } else {
                                recyclerAdapter.notifyItemInserted(electCodePackageVos3.size() - 1);
                                recyclerAdapter.notifyItemRangeChanged(electCodePackageVos3.size() - 1, 1);//更新的位置,更新的总条数
                            }
                            break;//找到扫描到的码,断开循环
                        }
                    }
                }else if(isContains(electCodePackageVos2,scanResult_1)){
                    Toast toast = Toast.makeText(context, "该码已经入库!", Toast.LENGTH_SHORT);
                    //放在左上角。如果你想往右边移动，将第二个参数设为>0；往下移动，增大第三个参数；后两个参数都只得像素
                    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                }else{
                    Toast toast = Toast.makeText(context, "单号中查询不到该码!", Toast.LENGTH_SHORT);
                    //放在左上角。如果你想往右边移动，将第二个参数设为>0；往下移动，增大第三个参数；后两个参数都只得像素
                    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                }
            }
        }
    }

    public void openScan(Context context) {
        barcodeDataReceiver = new BarcodeDataReceiver();
        IntentFilter mFilter = new IntentFilter("nlscan.action.SCANNER_RESULT");
        context.registerReceiver(barcodeDataReceiver, mFilter);
        Intent mIntent = new Intent("ACTION_BAR_SCANCFG");
        mIntent.putExtra("NON_REPEAT_TIMEOUT", Long.valueOf(5000));
        mIntent.putExtra("EXTRA_TRIG_MODE", 0);//连续扫描
        mIntent.putExtra("EXTRA_SCAN_MODE", 3);//广播输出模式
        context.sendBroadcast(mIntent);
        Intent mIntent2 = new Intent("ACTION_BAR_SCANCFG");
        mIntent2.putExtra("EXTRA_SCAN_NOTY_SND", 1);
        mIntent2.putExtra("EXTRA_SCAN_NOTY_VIB", 1);
        sendBroadcast(mIntent2);
    }

    public void closeScan(Context context) {
        context.unregisterReceiver(barcodeDataReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (electCodePackageVos3.size() == 0) {
            for (int i = 0; i < electCodePackageVos2.size(); i++) {
                if (electCodePackageVos2.get(i).getPosition() == application.getPosition()) {
                    electCodePackageVos3.add(electCodePackageVos2.get(i));
                }
            }
        }
        if (electCodePackageVos3.size() > 0) {
            recyclerAdapter = new RecyclerAdapter(electCodePackageVos3);
            rvStorage.setAdapter(recyclerAdapter);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (vp.getCurrentItem() == 0) {
                onBackPressed();
            } else if (vp.getCurrentItem() == 1) {
                startActivity(new Intent(context, SupplyStorageActivity.class));
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void isClearDialog() {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle("上级供货单号");
        alertBuilder.setMessage("上级供货单号已经重选,将会对之前的扫描进行清空，您确定吗?");
        alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                orderName=btnOrderNo.getText().toString();
                electCodePackageVos.clear();electCodePackageVos2.clear();electCodePackageVos3.clear();
                recyclerAdapter = new RecyclerAdapter(electCodePackageVos2);
                rvStorage.setAdapter(recyclerAdapter);
                vp.setCurrentItem(1);
                _alertDialog.dismiss();
            }
        });

        alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                _alertDialog.dismiss();
            }
        });
        _alertDialog = alertBuilder.create();
        _alertDialog.show();
    }

    private void initEvent() {
        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                if (tab.getPosition() == 1) {
                    setBtnToTemp();
                } else {
                    ScanUtil.powerOffScan(context);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
//                添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
//                再次选中tab的逻辑
            }
        });
        tabs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        adapter = new FragmentPagerAdapter(getSupportFragmentManager(), 1) {
            @Override
            public int getCount() {
                return mDatas.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mDatas.get(position);
            }
        };
        vp.setAdapter(adapter);
        tabs.setupWithViewPager(vp);
        TabLayout.Tab tab = tabs.getTabAt(0);
        ((View) tab.view).setBackgroundResource(R.drawable.tab_left);
        tab.setText("交货单");
        tab = tabs.getTabAt(1);
        ((View) tab.view).setBackgroundResource(R.drawable.tab_right);
        tab.setText("扫码入库");
    }

    private void setBtnToTemp() {
        if (btnOrderNo.getText().toString().equals("入库单号")||btnTransportEquip.getText().toString().equals("运输设备")||
                btnDeliveryDept.getText().toString().equals("选择配送单位")||btnPriceBook.getText().toString().equals("价格本")||btnTransportEquip.getText().toString().equals("运输设备")) {
            UtilHelper.alertMsg(context, "请配置好单据页面", "点击任意处关闭弹窗");
            vp.setCurrentItem(0);
        } else {
            if(btnOrderNo.getText().toString().equals(orderName)||orderName==null){
                orderName=btnOrderNo.getText().toString();
                startActivity(new Intent(context, SupplyStorageActivity.class));
                vp.setCurrentItem(1);
            }else{
                if(electCodePackageVos2.size()>0){
                    vp.setCurrentItem(0);
                    isClearDialog();
                }else{
                    if(!application.getOrderNoId().equals(application.getOrderNoId2())){
                        electCodePackageVos.clear();
                    }
                    orderName=btnOrderNo.getText().toString();
                    startActivity(new Intent(context, SupplyStorageActivity.class));
                    vp.setCurrentItem(1);
                }
            }
        }
    }

    private void initView() {
        initHeaderC("供应入库");
        vp = (ViewPager) findViewById(R.id.vpSheet);
        supplyBillFragment = new SupplyBillFragment();
        supplyContentFragment = new SupplyContentFragment();
        mDatas.add(supplyBillFragment);
        mDatas.add(supplyContentFragment);
        tabs = findViewById(R.id.tabSheet);
    }

    private void initHeaderC(String title) {
        if ("0".equals(application.getBackSystem())) title = title + "(测试版本)";
        TextView tvTitle = ((TextView) findViewById(R.id.screenTitle));
        if (tvTitle != null) tvTitle.setText(title);
        findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vp.getCurrentItem() == 0) {
                    onBackPressed();
                } else if (vp.getCurrentItem() == 1) {
                    startActivity(new Intent(context, SupplyStorageActivity.class));
                }
            }
        });
        String userName = application.getRealName() == null ? application.getUserName() : application.getRealName();
        ((TextView) findViewById(R.id.tvHeaderUserName)).setText(userName);
    }

    @Override
    public void fragmentCreated(View view) {
        super.fragmentCreated(view);
        btnOrderNo = (TextView) view.findViewById(R.id.btnOrderNo);
        btnSupplyDept = (TextView) view.findViewById(R.id.btnSupplyDept);
        btnVaccineType = (TextView) view.findViewById(R.id.btnVaccineType);
        btnPriceBook = (TextView) view.findViewById(R.id.btnPriceBook);
        btnStartTime = (TextView) view.findViewById(R.id.btnStartTime);
        btnEndTime = (TextView) view.findViewById(R.id.btnEndTime);
        btnTransportEquip = (TextView) view.findViewById(R.id.btnTransportEquip);
        btnDeliveryDept = (LastInputEditText) view.findViewById(R.id.btnDeliveryDept);
        btnToTemp = (Button) view.findViewById(R.id.btnToTemp);
        btnPriceBook.setOnClickListener((View.OnClickListener)context);
        btnOrderNo.setOnClickListener((View.OnClickListener) context);
        btnToTemp.setOnClickListener((View.OnClickListener) context);
        btnStartTime.setOnClickListener((View.OnClickListener) context);
        btnEndTime.setOnClickListener((View.OnClickListener) context);
        btnTransportEquip.setOnClickListener((View.OnClickListener) context);
        btnDeliveryDept.setOnClickListener((View.OnClickListener) context);
        btnDeliveryDept .addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                name.clear();id.clear();
                if(deptList2!=null&&isCheck==false){
                    for (RefDept refDept : deptList2) {
                        if(refDept.getName().contains(btnDeliveryDept.getText().toString())){
                            name.add(refDept.getName());
                            id.add(refDept.getId());
                        }
                    }
                    PopWindow.show(application, context,popup,isCheck, btnDeliveryDept, name, id, 7);
                }
            }
        });
        btnDeliveryDept.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    name.clear();id.clear();
                    if (deptList2 != null) {
                        for (RefDept refDept : deptList2) {
                            if(refDept.getName().contains(btnDeliveryDept.getText().toString())){
                                name.add(refDept.getName());
                                id.add(refDept.getId());
                            }
                        }
                        PopWindow.show(application, context,popup,isCheck, btnDeliveryDept, name, id, 7);
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void fragmentCreated2(View view) {
        super.fragmentCreated2(view);
        rvStorage = (RecyclerView) view.findViewById(R.id.rvStorage);
        btnSubmitVaccine = (Button) view.findViewById(R.id.btnSubmitVaccine);
        btnDelAllVaccine = (Button) view.findViewById(R.id.btnDelAllVaccine);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        rvStorage.setLayoutManager(layoutManager);
        btnSubmitVaccine.setOnClickListener((View.OnClickListener) context);
        btnDelAllVaccine.setOnClickListener((View.OnClickListener) context);
    }
    @Override
    protected void onPause() {
        super.onPause();
        ScanUtil.powerOffScan(context);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(vp.getCurrentItem()==1){
            ScanUtil.powerOnScan(context);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStartTime:
                getTimeDialog(v,0);
                break;
            case R.id.btnEndTime:
                getTimeDialog(v,1);
                break;
            case R.id.btnPriceBook://bill界面点击选择价格本
                name.clear();
                id.clear();
                if(priceBook!=null) {
                    for (int i = 0; i < priceBook.size(); i++) {
                        name.add(priceBook.get(i).getPriceBookNo());
                        id.add(priceBook.get(i).getId());
                    }
                    if(popup==null)createPopupWindow((TextView) v);
                    PopWindow.show(application, context,popup,isCheck, (TextView) v, name, id, 3);
                }else{
                    Toast.makeText(context,"未获取价格本信息,请稍后重试!",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnTransportEquip://下拉选择运输设备
                name.clear();id.clear();
                if(globalDictionaryList!=null){
                    for (GlobalDictionary globalDictionary : globalDictionaryList) {
                        name.add(globalDictionary.getValue());
                        id.add(globalDictionary.getKey());
                    }
                }else{
                    Toast.makeText(context,"没有获取到运输设备列表!",Toast.LENGTH_SHORT).show();
                }
                showChoose(btnTransportEquip,name,id,1);
                break;
            /*case R.id.btnDeliveryDept://下拉选择运输单位
                name.clear();id.clear();
                if(deptList2!=null){
                    for (RefDept refDept : deptList2) {
                        name.add(refDept.getName());
                        id.add(refDept.getRefDeptId());
                    }
                }else{
                    Toast.makeText(context,"没有获取到运输单位列表!",Toast.LENGTH_SHORT).show();
                }
                showChoose(btnDeliveryDept,name,id,2);
                break;*/
            case R.id.btnToTemp:
                vp.setCurrentItem(1);
                break;
            case R.id.btnOrderNo://上级交货单号
                if(superiorList==null){
                    Toast.makeText(context,"没有可用的出库单号!",Toast.LENGTH_SHORT).show();
                }else {
                    orderNoSelector();
                }
                break;
            case R.id.btnDelAllVaccine://删除所有扫中的条形码
                delAllVaccine();
                break;
            case R.id.btnSubmitVaccine://回到储位界面
                Intent stopIntent = new Intent("nlscan.action.STOP_SCAN");
                sendBroadcast(stopIntent);
                int i = electCodePackageVos3.size();
                if (i != 0) {
                    electCodePackageVos3.clear();
                    recyclerAdapter.notifyDataSetChanged();
                }
                startActivity(new Intent(context, SupplyStorageActivity.class));
                break;
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
    private void delAllVaccine(){
        for (int i = electCodePackageVos3.size() - 1; i > -1; i--) {
            if (electCodePackageVos3.get(i).getPosition() == application.getPosition()) {
                electCodePackageVos.add(electCodePackageVos3.get(i));
                electCodePackageVos2.remove(i + electCodePackageVos2.size() - electCodePackageVos3.size());
                electCodePackageVos3.remove(i);
                recyclerAdapter.notifyItemRemoved(i);
                recyclerAdapter.notifyItemRangeChanged(i, electCodePackageVos3.size() - i);
            }
        }
    }

    /*获取上级交货单的方法*/
    public void getOrderNo() {
        _progressDialog = new ProgressDialog(context);
        _progressDialog.setMessage("正在获取出库单号,请稍等...");
        _progressDialog.show();
        String deptId = application.getDeptId();
        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("deptId", deptId);
        String url = UtilHelper.baseUrls.EMIAO(application.getBackSystem());
        Retrofit retrofit = UtilHelper.retrofit(application, url);
        GetVaccineService vs = retrofit.create(GetVaccineService.class);
        String token = application.getAccessToken();
        Call<JsonForResult> jsonForResultCall = vs.getSuperiorList(token, map);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                if (_progressDialog != null) _progressDialog.cancel();
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            superiorList = JSONObject.parseArray(JSON.toJSONString(jsonForResult.getData()),SuperiorList.class);
                        }else{
                            Toast.makeText(context,"没有可用的出库单号!",Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        if (jsonForResult != null) {
                            UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                            alarm(jsonForResult.getMessage());
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                if (_progressDialog != null) _progressDialog.cancel();
                UtilHelper.alertMsg(context, "错误", t.getMessage());
            }
        });
    }


    private void orderNoSelector() {
        category.clear();
        orderNo.clear();
        id.clear();
        deptId.clear();
        sourceId.clear();
        for (int i = 0; i < superiorList.size(); i++) {
            orderNo.add(superiorList.get(i).getOrderNo());
            category.add(superiorList.get(i).getCategory());
            id.add(superiorList.get(i).getId());
            deptId.add(superiorList.get(i).getDeptId());
            sourceId.add(superiorList.get(i).getSourceId());
            createTime.add(superiorList.get(i).getCreateTime());
            transformUnit.add(superiorList.get(i).getTransformUnit());
            transformUnitName.add(superiorList.get(i).getTransformUnitName());
        }
        show(btnOrderNo,btnSupplyDept,btnVaccineType,btnStartTime,btnEndTime,btnDeliveryDept,createTime,orderNo,id,deptId,category,sourceId,transformUnit,transformUnitName);
    }


    public class RecyclerAdapter extends RecyclerView.Adapter<SupplyActivity.RecyclerAdapter.ViewHolder> {
        private List<ElectCodePackageVo> electCodePackageVo;

        public RecyclerAdapter(List<ElectCodePackageVo> electCodePackageVo) {
            this.electCodePackageVo = electCodePackageVo;
        }

        @NonNull
        @Override
        public SupplyActivity.RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vaccine, parent, false);
            SupplyActivity.RecyclerAdapter.ViewHolder viewHolder = new SupplyActivity.RecyclerAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull SupplyActivity.RecyclerAdapter.ViewHolder holder, int position) {
            holder.tvVaccineName.setText(electCodePackageVo.get(position).getVaccineName());
            holder.tv_num.setText(String.valueOf(electCodePackageVo.get(position).getQuantity()));
            String category = electCodePackageVo.get(position).getCategory();
            if (category.equals("1")) {
                holder.iv_category.setText("免规");
            } else if (category.equals("2")) {
                holder.iv_category.setText("非免规");
            } else if (category.equals("3")) {
                holder.iv_category.setText("配套品");
            }
            holder.tvManufactor.setText(electCodePackageVo.get(position).getManufacturerName());
            holder.tvTraceabilityCode.setText(electCodePackageVo.get(position).getElectCode());
            holder.ivDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    electCodePackageVos.add(electCodePackageVo.get(position));
                    electCodePackageVos2.remove(position + electCodePackageVos2.size() - electCodePackageVos3.size());
                    electCodePackageVos3.remove(position);
                    notifyItemRemoved(position);//移除该位置的项目
                    notifyItemRangeChanged(position, getItemCount() - position);//局部刷新从该position位置开始到剩余的所有项目
                }
            });
        }

        @Override
        public int getItemCount() {
            return electCodePackageVo.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvVaccineName, tvTraceabilityCode, tvManufactor, tv_num,iv_category; //疫苗名称，扫码数，交货单数,疫苗类型
            ImageView ivDel ;

            public ViewHolder(View itemView) {
                super(itemView);
                this.tvVaccineName = itemView.findViewById(R.id.tvVaccineName);
                this.ivDel = itemView.findViewById(R.id.ivDel);
                this.iv_category = itemView.findViewById(R.id.iv_category);
                this.tvTraceabilityCode = itemView.findViewById(R.id.tvTraceabilityCode);
                this.tvManufactor = itemView.findViewById(R.id.tvManufactor);
                this.tv_num = itemView.findViewById(R.id.tv_num);
            }
        }
    }

    /*销毁界面,将全局的list清空,返回上个界面的广播解绑*/
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeScan(context);
        electCodePackageVos2.clear();
        electCodePackageVos3.clear();
        electCodePackageVos.clear();
        getBackClose(this);
        application.clearBill();//将bill里的数据清除
    }
}